package cn.iocoder.yudao.module.system.service.homedata;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.system.dal.dataobject.curingemperature.CuringEmperatureDO;
import cn.iocoder.yudao.module.system.dal.dataobject.monitor.MonitorDO;
import cn.iocoder.yudao.module.system.dal.mysql.curingemperature.CuringEmperatureMapper;
import cn.iocoder.yudao.module.system.dal.mysql.monitor.MonitorMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.poi.hpsf.Array;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.homedata.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.homedata.HomeDataDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.homedata.HomeDataMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 首页数据 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class HomeDataServiceImpl implements HomeDataService {

    @Resource
    private MonitorMapper monitorMapper;
    @Resource
    private CuringEmperatureMapper curingEmperatureMapper;
    @Resource
    private HomeDataMapper homeDataMapper;

    @Override
    public Long createHomeData(HomeDataSaveReqVO createReqVO) {
        // 插入
        HomeDataDO homeData = BeanUtils.toBean(createReqVO, HomeDataDO.class);
        homeDataMapper.insert(homeData);
        // 返回
        return homeData.getId();
    }

    @Override
    public void updateHomeData(HomeDataSaveReqVO updateReqVO) {
        // 校验存在
        validateHomeDataExists(updateReqVO.getId());
        // 更新
        HomeDataDO updateObj = BeanUtils.toBean(updateReqVO, HomeDataDO.class);
        homeDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteHomeData(Long id) {
        // 校验存在
        validateHomeDataExists(id);
        // 删除
        homeDataMapper.deleteById(id);
    }

    @Override
        public void deleteHomeDataListByIds(List<Long> ids) {
        // 校验存在
        validateHomeDataExists(ids);
        // 删除
        homeDataMapper.deleteByIds(ids);
        }

    private void validateHomeDataExists(List<Long> ids) {
        List<HomeDataDO> list = homeDataMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(HOME_DATA_NOT_EXISTS);
        }
    }

    private void validateHomeDataExists(Long id) {
        if (homeDataMapper.selectById(id) == null) {
            throw exception(HOME_DATA_NOT_EXISTS);
        }
    }

    @Override
    public HomeDataDO getHomeData(Long id) {
        return homeDataMapper.selectById(id);
    }
    @Override
    public Map homeData() {
        Map data = new HashMap<>();
        // 获取当前日期
        String formatter = String.valueOf(LocalDate.now());

        // 定义日期格式
        List<MonitorDO> monitorDOS = monitorMapper.selectList(new QueryWrapper<MonitorDO>().eq("date",formatter));
        List<HomeDataDO> homeDataDOS = homeDataMapper.selectList(new QueryWrapper<HomeDataDO>().eq("date",formatter));
        List<CuringEmperatureDO> curingEmperatureDOS = curingEmperatureMapper.selectList(new QueryWrapper<CuringEmperatureDO>().eq("date",formatter));
        data.put("kjsc",null);
        data.put("trqzl",null);
        data.put("ydzl",null);
        data.put("ghwd",null);
        data.put("scxs",null);
        data.put("csjk",null);
        data.put("cn",null);
        data.put("rytr",null);
        for (MonitorDO item:monitorDOS) {
            if(item.getType().equals("1")){
                data.put("kjsc",item);
            }else if(item.getType().equals("6")){
                data.put("trqzl",item);
            }else if(item.getType().equals("7")){
                data.put("ydzl",item);
            }
        }
        for (HomeDataDO item:homeDataDOS) {
            if(item.getType().equals("1")){
                data.put("scxs",item);
            }else if(item.getType().equals("2")){
                data.put("rytr",item);
            }else if(item.getType().equals("3")){
                data.put("cn",item);
            }else if(item.getType().equals("4")){
                data.put("csjk",item);
            }
        }
        for (CuringEmperatureDO item:curingEmperatureDOS) {
            data.put("ghwd",item);
        }
        return data;
    }

    @Override
    public PageResult<HomeDataDO> getHomeDataPage(HomeDataPageReqVO pageReqVO) {
        return homeDataMapper.selectPage(pageReqVO);
    }

}
